package com.tusuo.stds.ui.account;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.biz.CurrentUser;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.ui.avatar.AvatarActivity_;
import com.tusuo.stds.ui.bindthird.BindThirdActivity_;
import com.tusuo.stds.ui.gender.SetGenderActivity_;
import com.tusuo.stds.ui.name.SetNameActivity_;
import com.tusuo.stds.ui.school.ProvinceActivity_;
import com.tusuo.stds.util.Utils;

import org.parceler.Parcels;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.1
 *
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(AccountPresenter.class)
class AccountPresenter extends MvpBasePresenter<AccountView> {

    @Inject
    CurrentUser mCurrentUser;
    @Inject
    ApiHelper mApiHelper;
    @Inject
    SharedPreferences mSharedPref;

    private Context mContext;
    private String mErrMsgServer;

    private UserInfo mUserInfo;
    private AreaList mAreaList;

    private boolean mWechatBindState = false;
    private boolean mQQBindState = false;
    private boolean mWeiboBindState = false;

    private Subscriber<UserInfo> mGetUserInfoSubscriber = null;
    private Subscriber<AreaList> mGetAreaListSubscriber = null;

    AccountPresenter(Context context) {
        mContext = context;
        mErrMsgServer = mContext.getResources().getString(R.string.err_msg_server);

        Injector.initAccountPresenterComponent().inject(this);
    }

    @Override
    public void attachView(AccountView view) {
        super.attachView(view);

        mGetAreaListSubscriber = new GetAreaListSubscriber();
        mApiHelper.getArea(mGetAreaListSubscriber);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetAreaListSubscriber != null) {
            mGetAreaListSubscriber.unsubscribe();
            mGetAreaListSubscriber = null;
        }
        if (mGetUserInfoSubscriber != null) {
            mGetUserInfoSubscriber.unsubscribe();
            mGetUserInfoSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    void updateUserInfo() {
        mGetUserInfoSubscriber = new GetUserInfoSubscriber();
        mApiHelper.getUserInfo(mGetUserInfoSubscriber);
    }

    void toAvatar() {
        if (mUserInfo != null) {
            Intent intent = new Intent();
            intent.setClass(mContext, AvatarActivity_.class);
            intent.putExtra("avatar", mUserInfo.Photo);
            mContext.startActivity(intent);
        }
    }

    void toName() {
        if (mUserInfo != null) {
            Intent intent = new Intent();
            intent.setClass(mContext, SetNameActivity_.class);
            intent.putExtra("name", mUserInfo.Name);
            mContext.startActivity(intent);
        }
    }

    void toGender() {
        if (mUserInfo != null) {
            Intent intent = new Intent();
            intent.setClass(mContext, SetGenderActivity_.class);
            intent.putExtra("gender", mUserInfo.Gender);
            mContext.startActivity(intent);
        }
    }

    void toSchool() {
        if (mUserInfo != null) {
            if (mAreaList == null) {
                Utils.UI.showToastShort("数据载入中，请稍候片刻");
                return;
            }

            Intent intent = new Intent();
            intent.setClass(mContext, ProvinceActivity_.class);
            intent.putExtra(UserInfo.EXTRA_NAME, Parcels.wrap(mUserInfo));
            mCurrentUser.setAreaList(mAreaList);
            mContext.startActivity(intent);
        }
    }

    void toBindThird() {
        if (mUserInfo != null) {
            Intent intent = new Intent();
            intent.setClass(mContext, BindThirdActivity_.class);
            intent.putExtra(UserInfo.EXTRA_NAME, Parcels.wrap(mUserInfo));
            mContext.startActivity(intent);
        }
    }

    private class GetUserInfoSubscriber extends Subscriber<UserInfo> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final UserInfo userInfo) {
            if (userInfo.checkState(mContext)) {
                mUserInfo = userInfo;

                if (!TextUtils.isEmpty(userInfo.Photo)) {
                    String imgUrl = Utils.getCorrectImageUrl(userInfo.Photo);

                    //判断SD卡里面是否存在图片文件
//                    if (avatarFileName.isEmpty() || !avatarFile.exists()) {

                        if (!TextUtils.isEmpty(userInfo.Photo)) {
                            if (getView() != null) {
                                getView().showAvatar(imgUrl, false);
                            }
                        }
                } else {
                    if (getView() != null) {
                        getView().showAvatar(R.drawable.img_avatar_default);
                    }
                }

                if (getView() != null) {
                    getView().setName(userInfo.Name);
                    getView().setGender(userInfo.Gender);
                    getView().setSchoolName(userInfo.SchoolName);
                }
            }
        }
    }

    private class GetAreaListSubscriber extends Subscriber<AreaList> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final AreaList list) {
            if (list.checkState(mContext)) {
                if (list.Total < 1 || list.Data.size()< 1) {
                    Timber.d("get area error!");
                } else {
                    if (mAreaList == null || !mAreaList.equals(list)) {
                        mAreaList = list;
                    }
                }
            }
        }
    }
}
